package org.zpmis.api.business;

import com.github.pagehelper.PageInfo;
import org.zpmis.common.base.AdminLoginUser;
import org.zpmis.common.base.BaseWebService;
import org.zpmis.common.exception.BusinessException;
import org.zpmis.common.result.ResultCode;
import org.zpmis.common.utils.StringUtil;
import org.zpmis.domain.contract.enums.ContractTemplateEnum;
import org.zpmis.domain.contract.model.ContractTemplate;
import org.zpmis.domain.contract.qo.AddContractTemplateQo;
import org.zpmis.domain.contract.qo.ChangeStatusQo;
import org.zpmis.domain.contract.qo.ContractTemplateListQo;
import org.zpmis.domain.contract.qo.EditContractTemplateQo;
import org.zpmis.domain.contract.service.ContractTemplateService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.zpmis.domain.contract.vo.ContractTemplateListVo;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with CodeGenerator
 * Description:
 * @author  Yang
 * Date: 2021-6-2
 * Time: 14:52:19
 */
@Service
@Slf4j
public class ContractTemplateWebService extends BaseWebService {
     @Reference
     private ContractTemplateService contractTemplateService;

     /**
      * 新增合同模版
      * @param addContractTemplateQo
      */
     public void addContractTemplate(AddContractTemplateQo addContractTemplateQo) throws BusinessException,Exception{
          //获取当前登录用户
          AdminLoginUser adminLoginUser = super.getAdminLoginUser();
          if(adminLoginUser == null){
               throw new BusinessException(ResultCode.USER_NOT_LOGIN);
          }
          contractTemplateService.save(
          ContractTemplate.builder()
                  .name(addContractTemplateQo.getName())
                  .type(addContractTemplateQo.getType())
                  .content(addContractTemplateQo.getContent())
                  .attachment(addContractTemplateQo.getAttachment())
                  .createTime(Instant.now().toEpochMilli())
                  .isDeleted(ContractTemplateEnum.NOT_DELETE.code())
                  .status(ContractTemplateEnum.ENABLE_STATUS.code())
                  .createId(adminLoginUser.getId())
                  .createPerson(adminLoginUser.getUserName())
                  .build()
          );

     }

     /**
      * 修改合同模版
      * @param editContractTemplateQo
      */
     public void editContractTemplate(EditContractTemplateQo editContractTemplateQo) {
          Long id = editContractTemplateQo.getId();
          ContractTemplate contractTemplate = contractTemplateService.findById(id);
          if(contractTemplate == null){
               throw new BusinessException(ResultCode.DATA_NOT_FOUND);
          }
          contractTemplateService.update(
                  ContractTemplate.builder()
                  .id(id)
                  .name(editContractTemplateQo.getName())
                  .type(editContractTemplateQo.getType())
                  .content(editContractTemplateQo.getContent())
                  .attachment(editContractTemplateQo.getAttachment())
                  .build()
          );
     }

     /**
      * 禁用或启用模板
      * @param changeStatusQo
      */
     public void changeStatus(ChangeStatusQo changeStatusQo) {
          Long id = changeStatusQo.getId();
          ContractTemplate contractTemplate = contractTemplateService.findById(id);
          if(contractTemplate == null){
               throw new BusinessException(ResultCode.DATA_NOT_FOUND);
          }
          ContractTemplate updateContractTemp = ContractTemplate.builder().id(id).build();
          Integer status = contractTemplate.getStatus();
          if(status == ContractTemplateEnum.ENABLE_STATUS.code()){
               updateContractTemp.setStatus(ContractTemplateEnum.DISABLE_STATUS.code());
          }
          if(status == ContractTemplateEnum.DISABLE_STATUS.code()){
               updateContractTemp.setStatus(ContractTemplateEnum.ENABLE_STATUS.code());
          }
          contractTemplateService.update(updateContractTemp);
     }

     /**
      * 查询合同模板列表
      * @param contractTemplateListQo
      * @return
      */
     public PageInfo contractTemplateList(ContractTemplateListQo contractTemplateListQo) throws Exception{
          String name = contractTemplateListQo.getName();
          Integer type = contractTemplateListQo.getType();
          Integer status = contractTemplateListQo.getStatus();
          Long startTime = contractTemplateListQo.getStartTime();
          Long endTime = contractTemplateListQo.getEndTime();
          Condition condition = new Condition(ContractTemplate.class);
          Example.Criteria criteria = condition.createCriteria();
          criteria.andEqualTo("isDeleted",ContractTemplateEnum.NOT_DELETE.code());

          if(StringUtil.isNotEmpty(name)){
               criteria.andLike("name","%"+name+"%");
          }
          if(type != null){
               criteria.andEqualTo("type",type);
          }
          if(status != null){
               criteria.andEqualTo("status",status);
          }
          if(startTime != null && endTime != null){
               criteria.andGreaterThanOrEqualTo("createTime",startTime);
               criteria.andLessThanOrEqualTo("createTime",endTime);
          }
          if(startTime != null && endTime == null){
               criteria.andGreaterThanOrEqualTo("createTime",startTime);
               criteria.andLessThanOrEqualTo("createTime",Instant.now().toEpochMilli());
          }
          if(startTime == null && endTime != null){
               criteria.andLessThanOrEqualTo("createTime",endTime);
          }
          condition.orderBy("createTime").desc();
          List<ContractTemplate> contractTemplateList = contractTemplateService.findByCondition(condition);
          PageInfo pageInfo = new PageInfo<>(contractTemplateList);
          //设置自定义返回对象
          List<ContractTemplateListVo> resultList = contractTemplateList.stream().map(ContractTemplateListVo::new).collect(Collectors.toList());
          pageInfo.setList(resultList);
          return pageInfo;
     }
}
